<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Configure Remote Device</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_kvrconfig.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Configure Remote Device </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\kvrConfig.c</b> </p><div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** This examples shows how to configure a remote device It is a console mode</span></div><div class="line"><span class="comment">** program that can scan for available networks, configure a local (USB)</span></div><div class="line"><span class="comment">** device, and use the new configuration to establish a connection to WLAN.</span></div><div class="line"><span class="comment">**/</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kvrlib_8h.html">kvrlib.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// List all connected devices</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">void</span> listDevices (<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat = <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>;</div><div class="line">  <span class="keywordtype">char</span> name[100];</div><div class="line">  <span class="keywordtype">int</span> channel_count = 0;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channel_on_card = 0;</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;channel_count);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;First argument must be a channel!\n\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;Channel\t Name\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;--------------------------------------------------------\n&quot;</span>);</div><div class="line">  <span class="keywordflow">for</span> (i = 0; (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) &amp;&amp; (i &lt; channel_count); i++) {</div><div class="line">    name[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line"></div><div class="line">    stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#afc7edc82e3d370444c3b83c66a9c6479">canCHANNELDATA_DEVDESCR_ASCII</a>, name, <span class="keyword">sizeof</span>(name));</div><div class="line">    <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printf(<span class="stringliteral">&quot;canGetChannelData(canCHANNELDATA_DEVDESCR_ASCII) failed.\n&quot;</span>);</div><div class="line">      <span class="keywordflow">continue</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#a4e0823a649d26191cde50f2c4fa5a65d">canCHANNELDATA_CHAN_NO_ON_CARD</a>, &amp;channel_on_card, <span class="keyword">sizeof</span>(channel_on_card));</div><div class="line">    <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printf(<span class="stringliteral">&quot;canGetChannelData(canCHANNELDATA_CHAN_NO_ON_CARD) failed.\n&quot;</span>);</div><div class="line">      <span class="keywordflow">continue</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;%d\t %s (Channel %u)\n&quot;</span>, i, name, channel_on_card);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Can we configure the device on this channel without password?</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">int</span> isPasswordFree (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channelNumber)</div><div class="line">{</div><div class="line">  kvrStatus       status;</div><div class="line">  kvrConfigHandle handle;</div><div class="line"></div><div class="line">  status = kvrConfigOpen(channelNumber, kvrConfigMode_R, <span class="stringliteral">&quot;&quot;</span>, &amp;handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Failed to open configuration with empty password on channel %u\n&quot;</span>, channelNumber);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    kvrConfigClose(handle);</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Wait until device appears (or timout occurs)</span></div><div class="line"><span class="comment">// Returns 0 if successful</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">int</span> waitForDevice(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ean_hi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ean_lo, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> serial,</div><div class="line">                  <span class="keywordtype">int</span> timeout_in_ms)</div><div class="line">{</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> time_start;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\nWaiting for device with EAN %08x%08x, and serial %u\n&quot;</span>, ean_hi, ean_lo, serial);</div><div class="line"></div><div class="line">  time_start = GetTickCount();</div><div class="line">  Sleep(2000);</div><div class="line"></div><div class="line">  <span class="keywordflow">do</span> {</div><div class="line">    <span class="keywordtype">int</span> channel_count;</div><div class="line">    <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> tmp_serial[2];</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> tmp_ean[2];</div><div class="line">    <span class="keywordtype">int</span> channel;</div><div class="line"></div><div class="line">    stat = <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;channel_count);</div><div class="line">    <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printf(<span class="stringliteral">&quot;canGetNumberOfChannels() failed.\n&quot;</span>);</div><div class="line">      <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> (channel=0; channel&lt;channel_count; channel++) {</div><div class="line">      stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel, <a class="code" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a>, tmp_ean, <span class="keyword">sizeof</span>(tmp_ean));</div><div class="line">      <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        printf(<span class="stringliteral">&quot;canGetChannelData(canCHANNELDATA_CARD_UPC_NO) failed.\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -2;</div><div class="line">      }</div><div class="line"></div><div class="line">      stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel, <a class="code" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>, tmp_serial, <span class="keyword">sizeof</span>(tmp_serial));</div><div class="line">      <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        printf(<span class="stringliteral">&quot;canGetChannelData(canCHANNELDATA_CARD_SERIAL_NO) failed.\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -3;</div><div class="line">      }</div><div class="line"></div><div class="line">      <span class="comment">//printf(&quot;%08x%08x %d\n&quot;, tmp_ean[1], tmp_ean[0], tmp_serial[0]);</span></div><div class="line">      <span class="keywordflow">if</span> (ean_hi == tmp_ean[1] &amp;&amp; ean_lo == tmp_ean[0] &amp;&amp; serial == tmp_serial[0]) {</div><div class="line">        printf(<span class="stringliteral">&quot;Found!\n\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> 0; <span class="comment">// Found!</span></div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;Try again...\n&quot;</span>);</div><div class="line">    Sleep(500);</div><div class="line">  } <span class="keywordflow">while</span> (GetTickCount() &lt; (time_start + timeout_in_ms));</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Device did not appear within given timeout\n\n&quot;</span>);</div><div class="line">  <span class="keywordflow">return</span> -4;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Can we configure the device on this channel (i.e. no-one else is using it)?</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">int</span> isAvailibleForConfig (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> canlib_channel, <span class="keyword">const</span> <span class="keywordtype">char</span> *password)</div><div class="line">{</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a>       hnd;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>       stat;</div><div class="line">  <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a>           bus_type;</div><div class="line">  <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a>           serial[2];</div><div class="line">  <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a>           tmp_serial[2];</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>   ean[2];</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>   tmp_ean[2];</div><div class="line">  <span class="keywordtype">int</span>             chan_no;</div><div class="line">  <span class="keywordtype">int</span>             tmp_chan;</div><div class="line">  kvrConfigHandle cfg_hnd;</div><div class="line"></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(canlib_channel, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line">  <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;Channel %u can not be opened exclusively.\n&quot;</span>, canlib_channel);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  stat = <a class="code" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl</a>(hnd, <a class="code" href="canlib_8h.html#a3a7d1321569e25fd9f8798a246f8c045">canIOCTL_GET_BUS_TYPE</a>, &amp;bus_type, <span class="keyword">sizeof</span>(bus_type));</div><div class="line">  <span class="keywordflow">if</span> (stat) {</div><div class="line">    printf(<span class="stringliteral">&quot;ERROR: failed to get bustype %d\n&quot;</span>, stat);</div><div class="line">    <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (bus_type != <a class="code" href="canlib_8h.html#aafac1a920ba6f66e52f79f369b1c6035">kvBUSTYPE_GROUP_LOCAL</a>) {</div><div class="line">    printf(<span class="stringliteral">&quot;Channel is not local (bus type:%lu).\n&quot;</span>, bus_type);</div><div class="line">    <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  }</div><div class="line">  <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// check all channels on a given device</span></div><div class="line">  <span class="comment">//</span></div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(canlib_channel, <a class="code" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a>, ean, <span class="keyword">sizeof</span>(ean));</div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(canlib_channel, <a class="code" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>, serial, <span class="keyword">sizeof</span>(serial));</div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(canlib_channel, <a class="code" href="canlib_8h.html#a4e0823a649d26191cde50f2c4fa5a65d">canCHANNELDATA_CHAN_NO_ON_CARD</a>, &amp;chan_no, <span class="keyword">sizeof</span>(chan_no));</div><div class="line"></div><div class="line">  tmp_chan = canlib_channel - chan_no;</div><div class="line">  <span class="keywordflow">while</span> (1) {</div><div class="line"></div><div class="line">    hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(tmp_chan, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line">    <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">      printf(<span class="stringliteral">&quot;Channel %d (same device as channel %u) can not be opened exclusively.\n&quot;</span>, tmp_chan, canlib_channel);</div><div class="line">      <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line"></div><div class="line">    tmp_chan++;</div><div class="line">    <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(tmp_chan, <a class="code" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a>, tmp_ean, <span class="keyword">sizeof</span>(tmp_ean));</div><div class="line">    stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(tmp_chan, <a class="code" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>, tmp_serial, <span class="keyword">sizeof</span>(tmp_serial));</div><div class="line">    <span class="keywordflow">if</span> (tmp_ean[0] != ean[0] || tmp_ean[1] != ean[1] ||</div><div class="line">        tmp_serial[0] != serial[0] || tmp_serial[1] != serial[1]) {</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (isPasswordFree(canlib_channel)) {</div><div class="line">    printf(<span class="stringliteral">&quot;No password is needed for configuring channel %u\n&quot;</span>, canlib_channel);</div><div class="line"></div><div class="line">    <span class="comment">// This test will remove the device from Kvaser Hardware</span></div><div class="line">    stat = kvrConfigOpen(canlib_channel, kvrConfigMode_RW, <span class="stringliteral">&quot;&quot;</span>, &amp;cfg_hnd);</div><div class="line">    <span class="keywordflow">if</span> (stat != kvrOK) {</div><div class="line">      printf(<span class="stringliteral">&quot;Failed to open configuration with empty password on channel %u\n&quot;</span>, canlib_channel);</div><div class="line">      <span class="keywordflow">return</span> 0;</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      kvrConfigClose(cfg_hnd);</div><div class="line">      <span class="comment">// Wait for the device to reappear in Kvaser Hardware</span></div><div class="line">      <span class="keywordflow">return</span> 0 == waitForDevice(ean[1], ean[0], serial[0], 10000); <span class="comment">// 10s timeout;</span></div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    printf(<span class="stringliteral">&quot;Password is needed for configuring channel %u\n&quot;</span>, canlib_channel);</div><div class="line"></div><div class="line">    <span class="comment">// This test will remove the device from Kvaser Hardware</span></div><div class="line">    stat = kvrConfigOpen(canlib_channel, kvrConfigMode_RW, password, &amp;cfg_hnd);</div><div class="line">    <span class="keywordflow">if</span> (stat != kvrOK) {</div><div class="line">      printf(<span class="stringliteral">&quot;Failed to open configuration with supplied password &#39;%s&#39; on channel %u\n&quot;</span>, password, canlib_channel);</div><div class="line">      <span class="keywordflow">return</span> 0;</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      kvrConfigClose(cfg_hnd);</div><div class="line">      <span class="comment">// Wait for the device to reappear in Kvaser Hardware</span></div><div class="line">      <span class="keywordflow">return</span> 0 == waitForDevice(ean[1], ean[0], serial[0], 10000); <span class="comment">// 10s timeout;</span></div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Scan for available networks and print some information about them</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line">kvrStatus doScanNetworks (kvrConfigHandle handle)</div><div class="line">{</div><div class="line">  kvrStatus status;</div><div class="line">  kvrStatus stat;</div><div class="line"></div><div class="line">  int32_t active   = 0;          <span class="comment">// is a passive scan</span></div><div class="line">  int32_t bss_type = kvrBss_ANY; <span class="comment">// infrastructure and adhoc</span></div><div class="line">  int32_t domain   = kvrRegulatoryDomain_WORLD;</div><div class="line"></div><div class="line">  <span class="keywordtype">char</span> ssid[40];</div><div class="line">  <span class="keywordtype">char</span> securityString[200];</div><div class="line">  int32_t rssi;</div><div class="line">  int32_t channel;</div><div class="line">  kvrAddress mac;</div><div class="line">  uint32_t capability;</div><div class="line">  uint32_t type_wpa;</div><div class="line">  kvrCipherInfoElement wpa_info;</div><div class="line">  kvrCipherInfoElement rsn_info;</div><div class="line"></div><div class="line">  status = kvrWlanStartScan(handle, active, bss_type, domain);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start scan (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">do</span> {</div><div class="line">    status = kvrWlanGetScanResults(handle, &amp;rssi, &amp;channel, &amp;mac, &amp;bss_type,</div><div class="line">                                   ssid, &amp;capability, &amp;type_wpa,</div><div class="line">                                   &amp;wpa_info, &amp;rsn_info );</div><div class="line">    <span class="keywordflow">if</span> (status == kvrOK) {</div><div class="line">      printf(<span class="stringliteral">&quot;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n&quot;</span>);</div><div class="line">      printf(<span class="stringliteral">&quot;SSID: %s\n&quot;</span>, ssid);</div><div class="line">      printf(<span class="stringliteral">&quot;RSSI: %d dBm\n&quot;</span>, rssi);</div><div class="line">      printf(<span class="stringliteral">&quot;WLAN Channel: %d\n&quot;</span>, channel);</div><div class="line">      printf(<span class="stringliteral">&quot;MAC address: %02X %02X %02X %02X %02X %02X\n&quot;</span>,</div><div class="line">             mac.address[0], mac.address[1], mac.address[2],</div><div class="line">             mac.address[3], mac.address[4], mac.address[5] );</div><div class="line">      printf(<span class="stringliteral">&quot;BSS type: %d\n&quot;</span>, bss_type);</div><div class="line">      printf(<span class="stringliteral">&quot;Capabilities: 0x%04x\n&quot;</span>, capability);</div><div class="line"></div><div class="line">      <span class="comment">// Convert to string</span></div><div class="line">      stat = kvrWlanGetSecurityText(securityString, <span class="keyword">sizeof</span>(securityString),</div><div class="line">                                    capability, type_wpa, &amp;wpa_info, &amp;rsn_info);</div><div class="line">      printf(<span class="stringliteral">&quot;Security&quot;</span>);</div><div class="line">      <span class="keywordflow">if</span> (stat == kvrERR_PARAMETER) {</div><div class="line">        printf(<span class="stringliteral">&quot;(truncated)&quot;</span>);</div><div class="line">      }</div><div class="line">      printf(<span class="stringliteral">&quot;: %s\n&quot;</span>, securityString);</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">while</span> ((status == kvrOK) || (status == kvrERR_NO_ANSWER));</div><div class="line"></div><div class="line">  <span class="comment">// kvrERR_BLANK =&gt; no more networks =&gt; OK</span></div><div class="line">  <span class="keywordflow">return</span> (status == kvrERR_BLANK ? kvrOK : status);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Configure a device</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line">kvrStatus doConfigure (kvrConfigHandle handle)</div><div class="line">{</div><div class="line">  kvrStatus status;</div><div class="line">  <span class="keywordtype">char</span> new_xml_config[4096];</div><div class="line">  <span class="keywordtype">char</span> old_xml_config[4096];</div><div class="line">  <span class="keywordtype">char</span> xml_error[2048];</div><div class="line"></div><div class="line">  <span class="comment">// Save the old configuration</span></div><div class="line">  status = kvrConfigGet(handle, old_xml_config, <span class="keyword">sizeof</span>(old_xml_config));</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not read configuration from device (%d)\n&quot;</span>, status);</div><div class="line">    kvrConfigClose(handle);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line">  printf(<span class="stringliteral">&quot;Old configuration: %s\n&quot;</span>, old_xml_config);</div><div class="line"></div><div class="line">  <span class="comment">// Adjust settings in XML file based on data from doScanNetworks()</span></div><div class="line">  <span class="comment">// ...</span></div><div class="line">  memcpy(new_xml_config, old_xml_config, <span class="keyword">sizeof</span>(old_xml_config));</div><div class="line"></div><div class="line">  <span class="comment">// Check that the new configuration is valid</span></div><div class="line">  memset(xml_error, 0, <span class="keyword">sizeof</span>(xml_error));</div><div class="line">  status = kvrConfigVerifyXml(new_xml_config, xml_error, <span class="keyword">sizeof</span>(xml_error));</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;The XML configuration is not valid (%d):\n%s\n&quot;</span>,</div><div class="line">    status, xml_error);</div><div class="line">    kvrConfigClose(handle);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Download new configuration</span></div><div class="line">  status = kvrConfigSet(handle, old_xml_config);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not write configuration to device (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> status;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Test the connection with the new configuration.</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line">kvrStatus doTryConfiguration (kvrConfigHandle handle, <span class="keywordtype">int</span> seconds)</div><div class="line">{</div><div class="line">  kvrStatus status;</div><div class="line"></div><div class="line">  kvrAddress address;</div><div class="line">  kvrAddress mac;</div><div class="line">  kvrAddress netmask;</div><div class="line">  kvrAddress gateway;</div><div class="line">  int32_t dhcp;</div><div class="line"></div><div class="line">  int32_t state;</div><div class="line">  int32_t tx_rate;</div><div class="line">  int32_t rx_rate;</div><div class="line">  int32_t channel;</div><div class="line">  int32_t rssi_mean;</div><div class="line">  int32_t tx_power;</div><div class="line"></div><div class="line">  kvrRssiHistory rssi[14] = {0};</div><div class="line">  kvrRttHistory rtt[14] = {0};</div><div class="line">  <span class="keywordtype">int</span> rtt_len = <span class="keyword">sizeof</span>(rtt) / <span class="keyword">sizeof</span>(kvrRttHistory);</div><div class="line">  <span class="keywordtype">int</span> rssi_len = <span class="keyword">sizeof</span>(rssi) / <span class="keyword">sizeof</span>(kvrRssiHistory);</div><div class="line">  <span class="keywordtype">int</span> rtt_actual;</div><div class="line">  <span class="keywordtype">int</span> rssi_actual;</div><div class="line"></div><div class="line">  <span class="comment">// connection test. 1 = activate ping</span></div><div class="line">  status = kvrNetworkConnectionTest(handle, 1);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start ping(%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">do</span> {</div><div class="line">    Sleep(1000);</div><div class="line">    <span class="comment">// Ask for RSSI and RTT so that we get updated</span></div><div class="line">    <span class="comment">// values when calling kvrNetworkGetConnectionStatus()</span></div><div class="line">    status = kvrNetworkGetRssiRtt(handle, rssi, rssi_len, &amp;rssi_actual,</div><div class="line">                                  rtt, rtt_len, &amp;rtt_actual);</div><div class="line">    <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">      printf(<span class="stringliteral">&quot;Could not get RSSI / RTT (%d)\n&quot;</span>, status);</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    status = kvrNetworkGetConnectionStatus(handle, &amp;state, &amp;tx_rate, &amp;rx_rate,</div><div class="line">                                           &amp;channel, &amp;rssi_mean, &amp;tx_power);</div><div class="line">    <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">      printf(<span class="stringliteral">&quot;Could not get status (%d)\n&quot;</span>, status);</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n&quot;</span>);</div><div class="line">    printf(<span class="stringliteral">&quot;Connection state: %d\n&quot;</span>, state);</div><div class="line">    printf(<span class="stringliteral">&quot;Transmit rate: %d kbit/s\n&quot;</span>, tx_rate);</div><div class="line">    printf(<span class="stringliteral">&quot;Receive rate: %d kbit/s\n&quot;</span>, rx_rate);</div><div class="line">    printf(<span class="stringliteral">&quot;Channel: %d\n&quot;</span>, channel);</div><div class="line">    printf(<span class="stringliteral">&quot;Receive Signal Strength Indicator: %d dBm\n&quot;</span>, rssi_mean);</div><div class="line">    printf(<span class="stringliteral">&quot;Transmit power level: %d dB\n&quot;</span>, tx_power);</div><div class="line">  } <span class="keywordflow">while</span> (--seconds &gt; 0);</div><div class="line"></div><div class="line">  <span class="comment">// connection test. 0 = deactivate ping</span></div><div class="line">  status = kvrNetworkConnectionTest(handle, 0);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not stop ping(%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  status = kvrNetworkGetAddressInfo(handle, &amp;address, &amp;mac, &amp;netmask, &amp;gateway, &amp;dhcp);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not get IP info(%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Assume IP v.4, i.e. address/netmask/gateway.type is kvrAddressType_IPV4</span></div><div class="line">  printf(<span class="stringliteral">&quot;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;DHCP: %s\n&quot;</span>, dhcp ? <span class="stringliteral">&quot;ON&quot;</span> : <span class="stringliteral">&quot;OFF&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;MAC address: %02X %02X %02X %02X %02X %02X\n&quot;</span>,</div><div class="line">         mac.address[0], mac.address[1], mac.address[2],</div><div class="line">         mac.address[3], mac.address[4], mac.address[5] );</div><div class="line">  printf(<span class="stringliteral">&quot;IP Address: %d.%d.%d.%d\n&quot;</span>, address.address[0], address.address[1],</div><div class="line">    address.address[2], address.address[3]);</div><div class="line">  printf(<span class="stringliteral">&quot;Netmask: %d.%d.%d.%d\n&quot;</span>, netmask.address[0], netmask.address[1],</div><div class="line">    netmask.address[2], netmask.address[3]);</div><div class="line">  printf(<span class="stringliteral">&quot;Gateway: %d.%d.%d.%d\n&quot;</span>, gateway.address[0], gateway.address[1],</div><div class="line">    gateway.address[2], gateway.address[3]);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> status;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Scan for available networks, configure a local (USB) device, and use the new</span></div><div class="line"><span class="comment">// configuration to establish a connection to WLAN.</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  <span class="keywordtype">char</span> *password = <span class="stringliteral">&quot;&quot;</span>; <span class="comment">//&quot;Secret&quot;;</span></div><div class="line"></div><div class="line">  kvrConfigHandle handle;</div><div class="line">  kvrStatus status;</div><div class="line">  <span class="keywordtype">int</span> canlib_channel;</div><div class="line"></div><div class="line">  <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> serial[2];</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> ean[2];</div><div class="line"></div><div class="line">  <span class="comment">// Initialize kvrlib</span></div><div class="line">  kvrInitializeLibrary();</div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <span class="keywordflow">switch</span> (argc) {</div><div class="line">  <span class="keywordflow">case</span> 1:</div><div class="line">    listDevices();</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  <span class="keywordflow">case</span> 2:</div><div class="line">    canlib_channel = argv[1][0] - <span class="charliteral">&#39;0&#39;</span>;</div><div class="line">    <span class="keywordflow">break</span>;</div><div class="line">  <span class="keywordflow">default</span>:</div><div class="line">    listDevices();</div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(canlib_channel, <a class="code" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a>, ean, <span class="keyword">sizeof</span>(ean));</div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(canlib_channel, <a class="code" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>, serial, <span class="keyword">sizeof</span>(serial));</div><div class="line"></div><div class="line">  <span class="comment">//----------------------------------------------------------------------------</span></div><div class="line">  <span class="comment">// Check configuration status</span></div><div class="line"></div><div class="line">  <span class="comment">// Can we configure the device?</span></div><div class="line">  <span class="keywordflow">if</span> (isAvailibleForConfig (canlib_channel, password)) {</div><div class="line">    printf(<span class="stringliteral">&quot;Channel %d is availible for configuration\n&quot;</span>, canlib_channel);</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    printf(<span class="stringliteral">&quot;Channel %d can not be opened for configuration.\n&quot;</span>, canlib_channel);</div><div class="line">    <a class="code" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary</a>();</div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line">  <a class="code" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary</a>();</div><div class="line"></div><div class="line">  <span class="comment">//----------------------------------------------------------------------------</span></div><div class="line">  <span class="comment">// Start configuration - read only</span></div><div class="line">  status = kvrConfigOpen(canlib_channel, kvrConfigMode_R, password, &amp;handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start config (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// List available networks. This information could be</span></div><div class="line">  <span class="comment">// helpful when creating the XML configuration.</span></div><div class="line">  status = doScanNetworks(handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Scan networks failed (%d)\n&quot;</span>, status);</div><div class="line">    kvrConfigClose(handle);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  kvrConfigClose(handle);</div><div class="line"></div><div class="line">  <span class="comment">//----------------------------------------------------------------------------</span></div><div class="line">  <span class="comment">// Start configuration - read/write</span></div><div class="line"></div><div class="line">  status = kvrConfigOpen(canlib_channel, kvrConfigMode_RW, password, &amp;handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start config (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Configure the device by writing the new XML configuration</span></div><div class="line">  status = doConfigure(handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not write new configuration (%d)\n&quot;</span>, status);</div><div class="line">    kvrConfigClose(handle);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Done!</span></div><div class="line">  kvrConfigClose(handle);</div><div class="line"></div><div class="line">  <span class="comment">// Wait for reboot</span></div><div class="line">  <span class="keywordflow">if</span> (waitForDevice(ean[1], ean[0], serial[0], 10000) != 0) { <span class="comment">//10s timeout</span></div><div class="line">    printf(<span class="stringliteral">&quot;waitForDevice() failed.\n&quot;</span>);</div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//----------------------------------------------------------------------------</span></div><div class="line">  <span class="comment">// Start configuration - read only</span></div><div class="line">  status = kvrConfigOpen(canlib_channel, kvrConfigMode_R, password, &amp;handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start config (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Test the new configuration for 5 s</span></div><div class="line">  status = doTryConfiguration(handle, 5);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;doTryConfiguration failed (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status;</div><div class="line">  }</div><div class="line"></div><div class="line">  kvrConfigClose(handle);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\nDone!\n&quot;</span>);</div><div class="line"></div><div class="line">  kvrUnloadLibrary();</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_kvrlib.html">Remote Device API (kvrlib)</a></li><li class="navelem"><a class="el" href="page_user_guide_kvrlib_samples.html">Sample Programs (kvrlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
